﻿// <copyright file="ModMessage.cs" company="SingularityShift">
// Copyright (c) 2009 SingularityShift.com
// </copyright>
// <author>Ben McClure</author>
// <email>ben@singularityshift.com</email>
// <date>2009-08-14</date>
// <summary>The primary implementation of an IModMessage, extending AbstractModMatcher.</summary>
namespace GameLib.Games.Components
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Text.RegularExpressions;

    /// <summary>
    /// <para>An AbstractModMatcher that implements the IModMessage interface.</para>
    /// <para>This represents a message about an IMod.</para>
    /// </summary>
    public class ModMessage : AbstractModMatcher, IModMessage
    {
        /// <summary>
        /// Initializes a new instance of the ModMessage class, specifying all parameters
        /// </summary>
        /// <param name="owner">The IMod which owns this message</param>
        /// <param name="path">The path to the mods this message should match</param>
        /// <param name="filePattern">The file pattern of the matches to look for</param>
        /// <param name="message">The message about this IMod to include</param>
        /// <param name="ifMissing">A bool indicating whether to match if it is missing or if it is found</param>
        public ModMessage(IMod owner, string path, string filePattern, string message, bool ifMissing)
        {
            Owner = owner;
            FilePath = path;
            FilePattern = filePattern;
            Message = message;
            this.IfMissing = ifMissing;
        }

        /// <summary>
        /// Initializes a new instance of the ModMessage class, specifying an owner and file pattern
        /// </summary>
        /// <param name="owner">The IMod which owns this message</param>
        /// <param name="filePattern">The file pattern of the matches to look for</param>
        public ModMessage(IMod owner, string filePattern)
            : this(owner, null, filePattern, null, false)
        {
        }

        /// <summary>
        /// Initializes a new instance of the ModMessage class, specifying an owner, path, and file pattern.
        /// </summary>
        /// <param name="owner">The IMod which owns this message</param>
        /// <param name="path">The path to the mods this message should match</param>
        /// <param name="filePattern">The file pattern of the matches to look for</param>
        public ModMessage(IMod owner, string path, string filePattern)
            : this(owner, path, filePattern, null, false)
        {
        }

        /// <summary>
        /// Initializes a new instance of the ModMessage class, specifying all values but owner as a string
        /// </summary>
        /// <param name="owner">The IMod which owns this message</param>
        /// <param name="path">The path to the mods this message should match</param>
        /// <param name="filePattern">The file pattern of the matches to look for</param>
        /// <param name="message">The message about this IMod to include</param>
        /// <param name="ifMissing">A string representation of a bool indicating whether to match if it is missing or if it is found</param>
        public ModMessage(IMod owner, string path, string filePattern, string message, string ifMissing)
            : this(owner, path, filePattern, message, ifMissing.Equals("1"))
        {
        }

        /// <summary>
        /// Initializes a new instance of the ModMessage class, specifying only an owner IMod
        /// </summary>
        /// <param name="owner">The IMod which owns this message</param>
        public ModMessage(IMod owner)
        {
            Owner = owner;
        }
        
        /// <summary>
        /// Initializes a new instance of the ModMessage class used for cloning
        /// </summary>
        public ModMessage()
        {
        }

        /// <summary>
        /// Gets or sets a value indicating whether to trigger a match if the file pattern does NOT exist (the opposite of the default behavior)
        /// </summary>
        public bool IfMissing
        {
            get;
            set;
        }

        /// <summary>
        /// Creates a deep copy of this ModMessage
        /// </summary>
        /// <returns>A new ModMessage object which is equal to this ModMessage, but is not the same object.</returns>
        public override object Clone()
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this);
            ms.Flush();
            ms.Position = 0;
            return (IModMessage) bf.Deserialize(ms);
        }
    }
}
